#include <iostream>
#include <vector>
#include <mutex>
#include <thread>

class ThreadSafeVector {
private:
    std::vector<int> data;
    std::vector<std::mutex> locks;

public:
    ThreadSafeVector(int n) : data(n), locks(n) {}

    // 设置指定索引的元素值，锁定对应的互斥量
    void set(int index, int value) {
        std::lock_guard<std::mutex> lock(locks[index]);
        data[index] = value;
    }

    // 获取指定索引的元素值，锁定对应的互斥量
    int get(int index) {
        std::lock_guard<std::mutex> lock(locks[index]);
        return data[index];
    }
};

void worker(ThreadSafeVector& vec, int index, int value) {
    vec.set(index, value);
    std::cout << "Set value at index " << index << " to " << value << std::endl;
}

int main() {
    ThreadSafeVector vec(10);
    std::thread t1(worker, std::ref(vec), 3, 100);
    std::thread t2(worker, std::ref(vec), 7, 200);

    t1.join();
    t2.join();

    return 0;
}